package com.spring.ext;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.*;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

/**
 * BeanDefinitionRegistryPostProcessor 的调用在 BeanFactoryPostProcessor 的调用 之前
 * 详情在：refresh().invokeBeanFactoryPostProcessors()中。
 */
@Component
public class MyBeanDefinitionRegisterPostProcessor implements BeanDefinitionRegistryPostProcessor{

	@Override
	public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
		System.out.println("MyBeanDefinitionRegisterPostProcessor -->postProcessBeanFactory:bean的数量:"+beanFactory.getBeanDefinitionNames().length);
	}

	/**
	 * BeanFactory是按照 BeanDefinitionRegistry 里记录的bean信息进行创建Bean实例的
	 * @param registry
	 * @throws BeansException
	 */
	@Override
	public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
		System.out.println("MyBeanDefinitionRegisterPostProcessor-->postProcessBeanDefinitionRegistry:bean的数量:"+registry.getBeanDefinitionNames().length);
		RootBeanDefinition rootBeanDefinition = new RootBeanDefinition(Blue.class);
		registry.registerBeanDefinition("hello",rootBeanDefinition);
	}
}
